package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

// Exists 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}

type FinalQuestions struct {
	ChoiceSingle             []Question `json:"choiceSingle"`
	ChoiceMultipleIndefinite []Question `json:"choiceMultipleIndefinite"`
	Judgement                []Question `json:"judgment"`
	ChoiceMultiple           []Question `json:"choiceMultiple"`
	Other                    []Question `json:"other"`
}

type Question struct {
	Type    string   `json:"type"`
	ID      string   `json:"id"`
	Options []Option `json:"options"`
	Title   string   `json:"title"`
	Answer  int      `json:"answer"`
}

type Option struct {
	Weight  int    `json:"weight"`
	Content string `json:"content"`
}

func amain() {
	jsonFile, err := os.Open("./out.json")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func(jsonFile *os.File) {
		err := jsonFile.Close()
		if err != nil {
			return
		}
	}(jsonFile)
	byteValue, _ := ioutil.ReadAll(jsonFile)
	var finalQuestions FinalQuestions
	err = json.Unmarshal(byteValue, &finalQuestions)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(len(finalQuestions.ChoiceMultipleIndefinite) + len(finalQuestions.ChoiceSingle) + len(finalQuestions.ChoiceMultiple) + len(finalQuestions.Judgement))
	fmt.Println(len(finalQuestions.Other))
	for _, question := range finalQuestions.Other {
		if len(question.Options) == 0 {
			question.Type = "judgment"
			finalQuestions.Judgement = append(finalQuestions.Judgement, question)
			continue
		}
		if len(question.Options) > 4 {
			question.Type = "choiceMultipleIndefinite"
			finalQuestions.ChoiceMultipleIndefinite = append(finalQuestions.ChoiceMultipleIndefinite, question)
			continue
		}
		if question.Answer == 1 || (question.Answer <= 8 && question.Answer%2 == 0) {
			question.Type = "choiceSingle"
			finalQuestions.ChoiceSingle = append(finalQuestions.ChoiceSingle, question)
			continue
		}
		question.Type = "choiceMultiple"
		finalQuestions.ChoiceMultiple = append(finalQuestions.ChoiceMultiple, question)
		continue
	}
	fmt.Println(len(finalQuestions.ChoiceMultipleIndefinite) + len(finalQuestions.ChoiceSingle) + len(finalQuestions.ChoiceMultiple) + len(finalQuestions.Judgement))
	fmt.Println(len(finalQuestions.Other))
	question := finalQuestions
	question.Other = nil
	jsonStr, err := json.Marshal(question)
	if err != nil {
		fmt.Println(err)
		return
	}
	normalStr := string(jsonStr)
	fmt.Println(normalStr)
	fileName := "./res.json"
	var file *os.File
	if Exists(fileName) {
		//使用追加模式打开文件
		file, err = os.OpenFile(fileName, os.O_APPEND, 0666)
		if err != nil {
			fmt.Println("Open file err =", err)
			return
		}
	} else {
		file, err = os.Create(fileName) //创建文件
		if err != nil {
			fmt.Println("file create fail")
			return
		}
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {
			return
		}
	}(file)
	//写入文件
	n, err := file.WriteString(normalStr)
	if err != nil {
		fmt.Println("Write file err =", err)
		return
	}
	fmt.Println("Write file success, n =", n)
}
